import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/admin',
        // name: 'home',
        component: HomeView,
        //
        redirect: '/admin/user/list',
        //因为被嵌套的<router-view>是在Homview中的
        //使用children来配置子级路由,children子集的配置方式与routers子级的配置方式来配置

        children: [
            {
                //通常，应该配置redirect属性，表示重定向，因为
                path: 'article/list',
                component: () => import('../views/article/ArticleListView.vue'),
                meta: {
                    title: '黑匣子博客管理平台 ----文章列表'
                }
            },
            {
                path: 'article/add-new',
                component: () => import('../views/article/ArticleAddNewView.vue'),
                meta: {
                    title: '酷鲨商城运营管理平台 ----添加文章'
                }
            },
            {
                path: 'user/list',
                component: () => import('../views/user/UserListView.vue'),
                meta: {
                    title: '博客运营管理平台 -- 用户管理',
                    requireAuth: true
                }
            },
            {
                path: 'role/list',
                component: () => import('../views/role/RoleListView.vue'),
                meta: {
                    title: '博客运营管理平台 -- 角色管理',
                    requireAuth: true
                }
            },
            {
                path: 'menu/list',
                component: () => import('../views/menu/MenuListView'),
                meta: {
                    title: '博客运营管理平台 -- 菜单管理',
                    requireAuth: true
                }
            },
            {
                path: 'comment/list',
                component: () => import('../views/comment/CommentListView'),
                meta: {
                    title: '博客运营管理平台 -- 评论管理',
                    requireAuth: true
                }
            },
            {
                path: 'tag/list',
                component: () => import('../views/tag/TagView'),
                meta: {
                    title: '博客运营管理平台 -- 标签管理',
                    requireAuth: true
                }
            },
            {
                path: 'category/list',
                component: () => import('../views/category/CategoryListView'),
                meta: {
                    title: '博客运营管理平台 -- 分类管理',
                    requireAuth: true
                }
            },
            {
                path: 'link/list',
                component: () => import('../views/link/LinkListView'),
                meta: {
                    title: '博客运营管理平台 -- 分类管理',
                    requireAuth: true
                }
            },
            {
                path: 'article/examine',
                component: () => import('../views/article/ArticleExamineView'),
                meta: {
                    title: '博客运营管理平台 -- 文章审核',
                    requireAuth: true
                }
            },
            {
                path: 'banner/list',
                component: () => import('../views/banner/BannerListView'),
                meta: {
                    title: '博客运营管理平台 -- 轮播图管理',
                    requireAuth: true
                }
            },

        ]
    },
    {
        path: '/about',
        name: 'about',
        component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    },

    {
        path: '/register',
        name: 'register',
        component: () => import(/* webpackChunkName: "about" */ '../views/RegisterView.vue')
    },
    {
        path: '/',
        redirect: '/admin/user/list',
        component: () => import('../views/HomeView.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/LoginView.vue')
    },
    {
        path: '/RegisterView',
        name: 'RegisterView',
        component: () => import('../views/RegisterView.vue')
    },
    {
        path: '/Verification',
        name: 'Verification',
        component: () => import('../views/login/verificationView.vue')
    }




    // 前台
    ,
    {
        path: '/a',
        name: 'a',
        component: () => import('../views/webapp/ArticleDetails.vue')
    },
    {
        path: '/b',
        name: 'b',
        component: () => import('../views/webapp/PersonalCenter.vue')
    },
    {
        path: '/d',
        name: 'd',
        component: () => import('../views/webapp/D.vue')
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

/*路由守卫*/
router.beforeEach((to,from,next)=>{
    if (to.meta.requireAuth){   // 需要登录权限进入的路由
        if (!localStorage.getItem("jwt")){     // 如果获取不到登录信息，返回登录
            router.replace({
                path: '/login',
                // 登录成功后跳入浏览的当前页面
                query: {redirect: router.currentRoute.fullPath}
            })

        }else {     // 获取到登录信息，进行下一步
            return next();
        }
    }else {     // 不需要登录权限的路由直接进行下一步
        return next();
    }
})

export default router
